<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24">

          <a-col :span="6">
            <a-form-item label="Code">
              <a-input placeholder="cluster code" v-model="queryParam.code"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="6">
            <a-form-item label="Name">
              <a-input placeholder="cluster name" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="6" >
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchByQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">添加</a-button>
      <a-button
        style="margin-left:8px"
        :disabled="selectedRowKeys.length == 0"
        ghost
        type="danger"
        icon="delete" @click="batchDel">批量删除</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>
        已选择&nbsp;<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>
      <a-table
        ref="table"
        bordered
        size="middle"
        :columns="columns"
        rowKey="code"
        :dataSource="dataSource"
        :pagination="pagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange"
      >

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="handleViewTopics(record)">Topics</a>
          <a-divider type="vertical" />
          <a @click="handleViewBrokers(record)">Brokers</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item >
                <a href="javascript:;" @click="handleDetail(record)">详情</a>
              </a-menu-item>

              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.code)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>

            </a-menu>
          </a-dropdown>
          </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <cluster-modal ref="modal" @ok="modalFormOk"></cluster-modal>

  </a-card>
</template>

<script>
  import kafkamanagerApi from '@/api/kafkamanager'
  import ClusterModal from './modules/ClusterModal'

  export default {
    name: 'cluster-list',
    components: {
      ClusterModal,
    },
    data () {
      return {
        loading: false,
        dataSource:[],
        queryParam: {},
        pagination:{
          current: 1,
          pageSize: 5,
          pageSizeOptions: ['5', '10', '25', '100'],
          showTotal: (total, range) => {
            return range[0] + "-" + range[1] + " 共" + total + "条"
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        selectedRowKeys: [],
        selectedRows: [],
        sorter:{
          column: 'createTime',
          order: 'desc',
        },
        columns: [{
          title: 'Code',
          dataIndex: 'code',
          sorter: true,
          width: '20%',
          sorter: (a, b) => a.code.length - b.code.length,
        }, {
          title: 'Name',
          dataIndex: 'name',
          sorter: true,
          width: '20%',
          sorter: (a, b) => a.name.length - b.name.length,
        }, {
          title: 'BootstrapServers',
          dataIndex: 'bootstrapServers',
        }, {
          title: 'ZookeeperServers',
          dataIndex: 'zookeeperServers',
        }, {
          title: '操作',
          scopedSlots: { customRender: 'action' },
          align:"center",
          width:250,
        }]
      }
    },
    mounted () {
      this.loadData()
    },
    methods: {
      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys;
        this.selectedRows = selectedRows;
      },
      onClearSelected() {
        this.selectedRowKeys = [];
        this.selectedRows = [];
      },
      searchByQuery(){
       this.loadData();
      },
      searchReset(){
        let that = this;
        Object.keys(that.queryParam).forEach(function(key){
          that.queryParam[key] = '';
        });
       that.loadData();
      },
      handleTableChange(pagination, filters, sorter){
        //TODO 筛选
        if (Object.keys(sorter).length>0){
          this.sorter.column = sorter.field;
          this.sorter.order = "ascend"==sorter.order?"asc":"desc"
        }
        this.pagination = pagination;
      },
      batchDel: function(){
        if(this.selectedRowKeys.length<=0){
          this.$message.warning('请选择一条记录！');
          return false;
        }else{
          let ids = "";
          let that = this;
          that.selectedRowKeys.forEach(function(val) {
            ids+=val+",";
          });
          that.$confirm({
            title:"确认删除",
            content:"是否删除选中数据?",
            onOk: function(){
              kafkamanagerApi.batchDeleteClusters({codes: that.selectedRowKeys}).then((res)=>{
                that.$message.success('删除成功！');
                that.loadData();
                that.onClearSelected();
              }).catch(err => {
                this.$message.error(err.message);
              });
            }
          });
        }
      },
      handleViewTopics: function(record) {
        this.$router.push({path: `/cluster/${record.code}/topics`})
      },
      handleViewBrokers: function(record) {
        this.$router.push({path: `/cluster/${record.code}/brokers`})
      },
      handleDelete: function(code){
        kafkamanagerApi.deleteCluster(code).then((res)=>{
          this.$message.success('删除成功！');
          this.loadData();
        }).catch(err => {
          this.$message.error(err.message);
        });
      },
      handleDetail:function(record){
        this.$refs.modal.edit(record);
        this.$refs.modal.title="详情";
        this.$refs.modal.disableSubmit = true;
      },
      handleEdit: function(record){
        this.$refs.modal.edit(record);
        this.$refs.modal.title="编辑";
      },
      handleAdd: function(){
        this.$refs.modal.add();
        this.$refs.modal.title="新增";
      },
      modalFormOk () {
        this.loadData();
      },
      loadData(){
        this.loading = true;
        kafkamanagerApi.listClusters().then(res => {
          this.dataSource = res;
        }).catch(err => {
          this.$message.error(err.message);
        }).finally(() => {
          this.loading = false;
        })
      },
    }
  }
</script>

<style scoped>
  .ant-card-body .table-operator{
    margin-bottom: 18px;
  }
  .ant-layout-content{
    margin:12px 16px 0 !important;
  }
  .ant-table-tbody .ant-table-row td{
    padding-top:15px;
    padding-bottom:15px;
  }
  .anty-row-operator button{margin: 0 5px}
  .ant-btn-danger{background-color: #ffffff}

  .ant-modal-cust-warp{height: 100%}
  .ant-modal-cust-warp .ant-modal-body{height:calc(100% - 110px) !important;overflow-y: auto}
  .ant-modal-cust-warp .ant-modal-content{height:90% !important;overflow-y: hidden}

  .anty-img-wrap{height:25px;position: relative;}
  .anty-img-wrap > img{max-height:100%;}
</style>
